using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mgh.Data.Sql;
using Jewelry.BussinessLayer;
using Jewelry.Entities;
using System.IO;

public partial class Pages_AEditProduct : Mgh.UI.Sql.PageBase
{
    #region properties

    protected string ProID
    {
        get { return Request.QueryString["pid"] == null ? string.Empty : Request.QueryString["pid"]; }
    }

    protected HttpPostedFile PostedFile
    {
        get { return ViewState["myfile"] == null ? null : ViewState["myfile"] as HttpPostedFile; }
        set { ViewState["myfile"] = value; } 
    }

    protected string ImageNameSaved
    {
        get { return ViewState["imgname"] == null ? string.Empty : ViewState["imgname"].ToString(); }
        set { ViewState["imgname"] = value; }
    }

    protected string PageIndex
    {
        get { return Request.QueryString["pin"] == null ? "0" : Request.QueryString["pin"]; }
    }

    protected string CategoryID
    {
        get { return Request.QueryString["cty"] == null ? "ALL" : Request.QueryString["cty"]; }
    }

    protected string Status
    {
        get { return Request.QueryString["sts"] == null ? "ALL" : Request.QueryString["sts"]; }
    }

    #endregion

    #region events

    override protected void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (upFile.HasFile)
        {
            PostedFile = upFile.PostedFile;
        }
        if (!IsPostBack)
        {
            LoadCategory();
            LoadFeature();
            if (!string.IsNullOrEmpty(ProID))
                this.bindData();
            ProductBLL bn = new ProductBLL(getCurrentConnect());
            
        }
    }

    protected void btSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (string.IsNullOrEmpty(this.ProID))
            {
                if (!this.InsertData())
                    return;
            }
            else
            {
                if (!this.UpdatData())
                    return;
            }
            //Page.ClientScript.RegisterStartupScript(typeof(String), "gotomanagerproduct", "parent.window.productlist.postback();", true);
            Response.Redirect("AManageProduct.aspx?pin=" + this.PageIndex + "&cty=" + this.CategoryID + "&sts=" + this.Status);
        }
    }

    protected void btCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("AManageProduct.aspx?pin=" + this.PageIndex + "&cty=" + this.CategoryID + "&sts=" + this.Status);
    }

    protected void btSaveContinue_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (string.IsNullOrEmpty(this.ProID))
            {
                if (!this.InsertData())
                    return;
            }
            else
            {
                if (!this.UpdatData())
                    return;
            }
            Response.Redirect("AEditProduct.aspx?pin=" + this.PageIndex + "&cty=" + this.CategoryID + "&sts=" + this.Status);
        }
    }

    #endregion

    #region functions

    private bool prepairData(ref DataSources.Jw_Tbl_ProductsRow row, ref DataSources.Jw_Tbl_ProFeatureDataTable tbF)
    {
        try
        {
            row.Model = txtCode.Text.Trim();
            row.ProID = this.ProID; 
            row.Ten = txtName.Text;
            row.MoTa = edDesc.Text;
            row.Gia = Int32.Parse(txtPrice.Text);
            row.TrangThai = dlStatus.SelectedValue;
            row.CateID = dlCate.SelectedValue;
            row.Hinh = this.ImageNameSaved;
            row.OriginalName = lbImage.Text.Trim();
            DataSources.Jw_Tbl_ProFeatureRow rowF;
            //foreach (ListItem item in cblFeature.Items)
            //{
            //    if (item.Selected)
            //    {
            //        rowF = tbF.NewJw_Tbl_ProFeatureRow();
            //        rowF.FeaID = item.Value;
            //        rowF.ProID = this.ProID;
            //        tbF.AddJw_Tbl_ProFeatureRow(rowF);
            //    }
            //}
        }
        catch (Exception ex) { cval.IsValid = false; cval.Text = ex.Message; return false; }
        return true;
    }

    private void bindData()
    {
        ProductBLL bn = new ProductBLL(getCurrentConnect());
        DataSources.Jw_Tbl_ProductsDataTable tb = new DataSources.Jw_Tbl_ProductsDataTable();
        DataSources.Jw_Tbl_ProFeatureDataTable tbF = new DataSources.Jw_Tbl_ProFeatureDataTable();
        if (bn.getFullProduct(ref tb, ref tbF, this.ProID))
        {
            txtCode.Text = tb[0].IsModelNull() ? string.Empty : tb[0].Model;
            txtName.Text = tb[0].IsTenNull() ? string.Empty : tb[0].Ten;
            txtPrice.Text = tb[0].IsGiaNull() ? string.Empty : tb[0].Gia.ToString();
            edDesc.Text = tb[0].IsMoTaNull() ? string.Empty : tb[0].MoTa;
            lbImage.Text = tb[0].IsOriginalNameNull() ? string.Empty : tb[0].OriginalName;
            this.ImageNameSaved = tb[0].IsHinhNull() ? string.Empty : tb[0].Hinh;
            lbImage.Attributes.Add("imgsrc", bn.getImgURL(this.ImageNameSaved));
            viewImage.ImageUrl = bn.getImgURL(this.ImageNameSaved, 850, 500, false);
            
            if (!tb[0].IsTrangThaiNull())
                dlStatus.SelectedValue = tb[0].TrangThai;

            if (!tb[0].IsCateIDNull())
                dlCate.SelectedValue = tb[0].CateID;

            //foreach (DataSources.Jw_Tbl_ProFeatureRow row in tbF)
            //{
            //    cblFeature.Items.FindByValue(row.FeaID).Selected = true;
            //}
        }
        else
        {
            cval.IsValid = false;
            cval.Text = bn.getMessage();
        }
    }

    private void LoadCategory()
    {
        CategoryBLL bn = new CategoryBLL(getCurrentConnect());
        DataSources.Jw_Tbl_CategoryDataTable tb = bn.getCategories();
        if (tb != null)
        {
            dlCate.DataSource = tb;
            dlCate.DataTextField = tb.TenColumn.ColumnName;
            dlCate.DataValueField = tb.CateIDColumn.ColumnName;
            dlCate.DataBind();
        }
        else
        {
            cval.IsValid = false;
            cval.Text = bn.getMessage();
        }
    }

    private void LoadFeature()
    {
        //ProductBLL bn = new ProductBLL(getCurrentConnect());
        //DataSources.Jw_Tbl_FeatureDataTable tb = bn.getFeatures();
        //if (tb != null)
        //{
        //    cblFeature.DataSource = tb;
        //    cblFeature.DataTextField = tb.TenColumn.ColumnName;
        //    cblFeature.DataValueField = tb.FeaIDColumn.ColumnName;
        //    cblFeature.DataBind();
        //}
        //else
        //{
        //    cval.IsValid = false;
        //    cval.Text = bn.getMessage();
        //}
    }

    private bool InsertData()
    {
        DataSources.Jw_Tbl_ProductsDataTable tb = new DataSources.Jw_Tbl_ProductsDataTable();
        DataSources.Jw_Tbl_ProductsRow row = tb.NewJw_Tbl_ProductsRow();
        DataSources.Jw_Tbl_ProFeatureDataTable tbF = new DataSources.Jw_Tbl_ProFeatureDataTable();
        if (this.prepairData(ref row, ref tbF))
        {
            ProductBLL bn = new ProductBLL(getCurrentConnect());
            if (!bn.addFullProduct(row, this.PostedFile, tbF))
            {
                cval.IsValid = false;
                cval.Text = bn.getMessage();
                
            }
            else return true;
        }
        return false;
    }

    private bool UpdatData()
    {
        DataSources.Jw_Tbl_ProductsDataTable tb = new DataSources.Jw_Tbl_ProductsDataTable();
        DataSources.Jw_Tbl_ProductsRow row = tb.NewJw_Tbl_ProductsRow();
        DataSources.Jw_Tbl_ProFeatureDataTable tbF = new DataSources.Jw_Tbl_ProFeatureDataTable();
        if (this.prepairData(ref row, ref tbF))
        {
            ProductBLL bn = new ProductBLL(getCurrentConnect());
            if (!bn.updateFullProduct(row, this.PostedFile, tbF))
            {
                cval.IsValid = false;
                cval.Text = bn.getMessage();
            }
            else return true;
        }
        return false;
    }

    #endregion

}
